package com.oa.web;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;

import com.google.gson.Gson;


@WebServlet("/upload")
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		Map<String, String> returnMap = new HashMap<String, String>();
////        String a=request.getParameter("a");//取出form-data中a的值
////        String b=request.getParameter("b");//取出form-data中a的值
//        //取出form-data中的二进制字段
//        MultipartHttpServletRequest multipartRequest=mRequest;  
//        MultipartFile multipartFile = multipartRequest.getFile("photo");//file是form-data中二进制字段对应的name
//        System.out.println(multipartFile.getSize());    
//        Map<String, Object> resultMapsReturn = new HashMap<>();
//        String imagePath="D:/1.jpg";//把取出来的二进制保存图片到本地
//        if(multipartFile.getSize()<=0){
//            resultMapsReturn.put("resultcode", "0");
////          resultMapsReturn.put("msg",  DisWebConst.ERROR_TITLE);
//            System.out.println("失败");
//        }else{
//            InputStream is = multipartFile.getInputStream();
//            OutputStream out = new FileOutputStream(imagePath);
//            IOUtils.copy(is, out);
//            is.close();
//            out.close();
//            System.out.println("成功");
//        }
		
		request.setCharacterEncoding("utf-8");
        String savePath = request.getServletContext().getRealPath("/uploads");
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            List<FileItem> items = upload.parseRequest(new ServletRequestContext(request));
            for (FileItem item : items) {
                if (!item.isFormField()) {
                     System.out.println("savePath = " + savePath);
                     System.out.println(item.getName());
                     System.out.println(item.getFieldName());
                    item.write(new File(savePath + "/" + item.getFieldName()+".gif"));
                }
                Map<String,String> result=new HashMap<String, String>();
                result.put("status", "ok");
                response.getWriter().write(new Gson().toJson(result));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
